<template>
  <el-upload
    class="upload-files"
    :headers="headers"
    :action="$store.getters.updateURL + '/content/importExcel'"
    :limit="1"
    :on-success="handleUpload"
    :on-progress="handleProgress"
    :on-error="handleError"
    :data="params"
    :show-file-list="false"
    ref="upload"
  >
    <el-button type="secondary">{{ $t('批量上传') }}</el-button>
  </el-upload>
</template>
<script>
export default {
  props: ['lang'],
  data() {
    return {
      headers: { Authorization: `Bearer ${this.$store.getters.token}` },
      params: {
        lang: ''
      }
    }
  },
  watch: {
    lang: {
      handler(data) {
        this.params = { lang: data }
      },
      immediate: true
    }
  },
  created() {},
  methods: {
    // 上传时开启loading
    handleProgress(res) {
      console.log(res)
      this.$emit('update:loading', true)
    },
    handleUpload(res) {
      this.$refs.upload.clearFiles()
      if (res.code == 0) {
        this.$emit('update:loading', false)
        // 上传成功更新列表
        this.$emit('success')
      } else {
        this.$emit('update:loading', false)
      }
    },
    handleError() {
      this.$emit('update:loading', false)
    }
  }
}
</script>
<style scoped lang="scss">
.upload-files {
  display: inline-block;
}
</style>
